package com.tyqx.hashchainbackend.controller;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbackend.service.SysPermissionService;
import com.tyqx.hashchainbackend.service.SysRolePermissionService;
import com.tyqx.hashchainbean.vo.PerOrResourceParamVO;
import com.tyqx.hashchainbean.vo.PerOrResourceResultVO;
import com.tyqx.hashchainbean.vo.SysPermissionVO;
import hashchainutil.common.BackendPage;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Resource
    SysPermissionService sysPermissionService;

    @Resource
    SysRolePermissionService sysRolePermissionService;

    @RequestMapping("/getAllPer")
    @ResponseBody
    private BackendPage<List<SysPermissionVO>> getAllPer(Integer page, Integer limit){
        try {
            Integer total=sysPermissionService.getAllPerCou();
            List<SysPermissionVO> list=sysPermissionService.getAllPer(page,limit);
            BackendPage<List<SysPermissionVO>> result=new BackendPage(total,list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new BackendPage("异常");
        }
    }

    @RequestMapping("/queryPermission")
    @ResponseBody
    public String queryPermission(@RequestParam(value = "parentId", required = false) Integer parentId,
                                  @RequestParam(value = "roleId") Long roleId) {
        try {
            List<PerOrResourceResultVO> sysPermissions;
            sysPermissions = sysPermissionService.getListByMap(parentId);
            List<PerOrResourceParamVO> sysRolePermissions = sysRolePermissionService.queryRolePermission(roleId);
            List<PerOrResourceParamVO> sysRolePers = new ArrayList<>();
            for (PerOrResourceParamVO vo : sysRolePermissions) {
                List<Long> perIds = sysPermissionService.queryPerId(vo.getPerOrRrId());
                if (perIds.isEmpty()) {
                    sysRolePers.add(vo);
                }
            }

            //调用方法实现角色树
//            return TreeNode.createComboTreeTree(sysPermissions, sysRolePers, 0l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/permissons/{id}",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public Dto permissons(@PathVariable("id") Integer perId) {
        List<SysPermissionVO> sysPermissions = sysPermissionService.getSysPermissionList(perId);
        return DtoUtil.returnDataSuccess(sysPermissions,"");
    }

    /*
    增加权限
     */
    @RequestMapping(value = "/permissonAdd",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public void permissonAdd(Integer perParentId, String perName, String createBy) {
        int i = sysPermissionService.pAdd(perParentId, perName, createBy);
        System.out.println(i + "-----------");
    }

    /*
    修改权限
     */
    @RequestMapping(value = "/permissonModify",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public void permissonModify(Integer perId, String perName, String modifyBy) {
        int i = sysPermissionService.pModify(perId, perName, modifyBy);
        System.out.println(i + "-----------");
    }

    /*
    删除权限
     */
    @RequestMapping(value = "/permissonRemove",
            method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public void permissonRemove(Integer perId) {
        int i = sysPermissionService.pRemove(perId);
        System.out.println(i + "-----------");
    }

    /**
     * 删除角色已拥有的权限
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/removePerOrRr")
    @ResponseBody
    public Dto removePerOrRr(@RequestParam("roleId") Long roleId, @RequestParam("rrId") Long rrId) {
        try {
            List<Long> longs = sysPermissionService.queryPerId(rrId);
            longs.add(rrId);
            if (sysRolePermissionService.removeById(roleId, longs)) {
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("删除失败", "100000");
    }

    /**
     * 查询用户已分配的权限
     *
     * @param roleId
     * @param rrId
     * @return
     */
    @RequestMapping("/queryPerOrRr")
    @ResponseBody
    public Dto queryPerOrRr(@RequestParam("roleId") Long roleId, @RequestParam("rrId") Long rrId) {
        List<PerOrResourceParamVO> sysRolePermission = null;
        try {
            sysRolePermission = sysRolePermissionService.queryRolePermission(roleId);
            for (PerOrResourceParamVO vo : sysRolePermission) {
                if (vo.getPerOrRrId() == rrId) {
                    return DtoUtil.returnSuccess();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("", "100000");
    }



}
